package com.esperia09.webservice.mixibot;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.esperia09.webservice.mixibot.oauth.GetToken;
import com.esperia09.webservice.mixibot.voice.VoiceTweet;

@SuppressWarnings("serial")
public class Mixi_botServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		PrintWriter out = resp.getWriter();

		// Authorization Code の取得
		String authCode = SessionIO.getMixiAuthorizationCode(req);
		
		// accessTokenを取得する
		GetToken tokenGetter = GetToken.getInstance();
		String accessToken = tokenGetter.getAccessToken(authCode);
		if(accessToken == null) {
			// accessTokenが取れなかったら、ログイン画面へリダイレクト
			resp.sendRedirect("/login");
		}

		// voice処理
		VoiceTweet voice = new VoiceTweet(accessToken);
		voice.postTweet("てすと！！ from mixi-bot");
	}
}
